﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace dbproj
{
    public partial class Form3 : Form
    {
        string SID;
        public Form3(string sID)
        {
            SID = sID;
            InitializeComponent();
            toolStripStatusLabel3.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            toolStripStatusLabel1.Text = $"欢迎学号为{SID}的同学登录选课系统";
            timer1.Start();
            Table();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            toolStripStatusLabel3.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }

        private void Form3_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();//结束整个程序.
        }

        public void Table()
        {
            dataGridView1.Rows.Clear();
            string sql = "select * from Course";
            Dao dao = new Dao();
            IDataReader dr = dao.read(sql);
            while (dr.Read())
            {
                string a, b,c,d;
                a = dr["Id"].ToString();
                b = dr["Name"].ToString();
                c = dr["Teacher"].ToString();
                d = dr["Credit"].ToString();
                string[] str = { a, b, c, d };
                dataGridView1.Rows.Add(str);
            }
            dr.Close();//关闭读取
        }

        private void 选课ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string cID = dataGridView1.SelectedCells[0].Value.ToString(); //选中的课程号
            // 判读是否已经选过该课
            string sqlCheck = $"select * from CourseRecord where sId='{SID}' and cId = '{cID}'";
            Dao daoC = new Dao();
            IDataReader dc = daoC.read(sqlCheck);
            if(!dc.Read())
            {
                string sql = $"Insert into CourseRecord values('{SID}','{cID}')";
                //MessageBox.Show(sql);
                Dao dao = new Dao();
                int i = dao.Execute(sql);
                if (i > 0)
                {
                    MessageBox.Show("选课成功");
                }
            }
            else
            {
                MessageBox.Show("不允许重复选课!");
            }
        }

        private void 我的课程ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form31 f= new Form31(SID);
            f.Show();
        }
    }
}
